import fs from 'fs'
import SQLiteDB from './wrapper/SQLiteDB'

class ConfigManager {
  db: SQLiteDB
  isMenuBarVisible: boolean = false
  windowState: string = ''
  constructor() {
    fs.mkdir('./tmp', () => {})
    this.db = new SQLiteDB('tmp/config.db')
    this.load()
  }
  load() {
    const db = this.db
    db.run(`CREATE TABLE IF NOT EXISTS config (key TEXT INTEGER PRIMARY KEY, value TEXT)`)
    const configs: any = db.all('SELECT * FROM config')
    for (const { key, value } of configs) {
      if (key === 'isMenuBarVisible') this.isMenuBarVisible = value === 'true'
      if (key === 'windowState') this.windowState = value
    }
  }
  saveConfig(key: string, value: string) {
    const db = this.db
    db.run(`INSERT OR REPLACE INTO config (key, value) VALUES (?, ?)`, [key, value])
  }
  save() {
    this.saveConfig('isMenuBarVisible', this.isMenuBarVisible ? 'true' : 'false')
    this.saveConfig('windowState', this.windowState)
  }
  close() {
    this.db.close()
  }
}

export default ConfigManager
